const state = {
  token: '',
  userInfo: {},
}

const mutations = {
  // 登录
  USERLOGIN(state, value) {
    state.token = value
  },
  // 存储用户信息
  GETUSERINFO(state, value) {
    state.userInfo = value
  },
  // 清除用户信息
  CLEAR(state) {
    state.userInfo = {}
    state.token = ''
  },
}

const actions = {
  // 注册
  async reqRegister({ commit }, payload) {
    const ret = await reqUserRegister(payload)
    if (ret.code == 200) {
      return 'ok'
    } else {
      return Promise.reject(new Error(ret.msg))
    }
  },
  // 登录
  async reqLogin({ commit }, payload) {
    const ret = await reqUserLogin(payload)
    if (ret.code == 200) {
      commit('USERLOGIN', ret.token)
      return 'ok'
    } else {
      return Promise.reject(new Error(ret.msg))
    }
  },
  // 获取用户信息
  async reqUserInformation({ commit }) {
    const ret = await reqUserInfo()
    if (ret.code == 200) {
      commit('GETUSERINFO', ret.data)
      return 'ok'
    } else {
      return Promise.reject(new Error(ret.msg))
    }
  },
  // 更新用户信息
  async reqUpdateUserInformation({ commit }, payload) {
    const ret = await reqUpdateUserInfo(payload)
    if (ret.code == 200) {
      return 'ok'
    } else {
      return Promise.reject(new Error(ret.msg))
    }
  },
}

//计算属性
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
}
